package com.yjd.pub.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.base.model.CityModel;
import com.yjd.comm.base.service.ICityService;
import com.yjd.pub.base.dao.ICityDao;

/**
 * <b>description</b>：城市业务实现<br>
 * <b>time</b>：2017-06-26 13:27:12 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("cityService")
public class CityServiceImpl extends BaseServiceImpl implements ICityService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public CityModel insert(CityModel model) throws Exception {
		return this.cityDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(CityModel model) throws Exception {
		return this.cityDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.cityDao.deleteModel(paramMap, true);
	}

	@Override
	public List<Long> querySubCities(Long regionId, List<Long> ids) throws Exception {
		ids.add(regionId);

		List<CityModel> cities = cityDao.getModelList(FrameUtil.newHashMap("parentid", regionId), DbWREnums.READ.isWrite());
		if(null == cities || cities.size() == 0){
			return ids;
		}

		for(CityModel city : cities){
			querySubCities(city.getId(), ids);
		}

		return ids;
	}

	@Resource
	private ICityDao cityDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.cityDao;
	}

}
